package com.all.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.all.base.BaseActivity;
import com.all.ui.adapter.ListBaseAdapter;
import com.all.utils.ToastUtils;
import com.android.AndroidApplication;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

	private ListView mListView;
	long waitTime = 2000;
    long touchTime = 0;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}
	
	/**
	 * 初始化
	 */
	private void initView() {
		// TODO Auto-generated method stub
		mListView = (ListView)findViewById(R.id.lv_demos);
		MainActivityAdapter mAdapter = new MainActivityAdapter(this) ;
		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				 @SuppressWarnings("unchecked")
				Map<String, Object> map = (Map<String, Object>)parent.getItemAtPosition(position);
			     Intent intent = (Intent) map.get("intent");
			     startActivity(intent);
			     //过场动画
				// overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
			}
		});
		mAdapter.addItem(getListData());
		mListView.setAdapter(mAdapter);
	}
	/**
	 * 初始化列表数据
	 * @return
	 */
	protected List<Map<String, Object>> getListData(){
		List<Map<String, Object>> mListViewData = new ArrayList<Map<String, Object>>();
		Intent  mainIntent = new Intent(Intent.ACTION_MAIN,null);
		mainIntent.addCategory("android.all.LAUNCHER");
		//mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> mActivityList = getPackageManager().queryIntentActivities(mainIntent, PackageManager.GET_INTENT_FILTERS);
		for (int i = 0; i < mActivityList.size(); i++) 
        {
			 ResolveInfo info = mActivityList.get(i);
			 String label = info.loadLabel(getPackageManager()) != null? info.loadLabel(getPackageManager()).toString():info.activityInfo.name;
			  Map<String, Object> temp = new HashMap<String, Object>();
			  temp.put("title", label);
			  temp.put("intent", buildIntent(info.activityInfo.packageName, info.activityInfo.name));
			  mListViewData.add(temp);
        }
		return mListViewData;
		
	}
	
	/**
	 * 构建每一个Item点击Intent
	 * @param packageName
	 * @param componentName
	 * @return
	 */
    protected Intent buildIntent(String packageName, String componentName) {
        Intent result = new Intent();
        result.setClassName(packageName, componentName);
        return result;
    }
	/**
	 * 监听[返回]键事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 返回键
		if (KeyEvent.KEYCODE_BACK == keyCode) {

			long currentTime = System.currentTimeMillis();
			if ((currentTime - touchTime) >= waitTime) {
				ToastUtils.showShort("再按一次，退出程序");
				touchTime = currentTime;
			} else {
				((AndroidApplication) getApplicationContext()).removeAll();
			}
			return true;
		}
		return false;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 列表适配器
	 */
	protected class MainActivityAdapter extends ListBaseAdapter{

		public MainActivityAdapter(Activity mContext) {
			// TODO Auto-generated constructor stub
			super(mContext);
		}

		
		@SuppressWarnings("unchecked")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Holder mHolder = null;
			if(null== convertView)
			{
				convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_main_activit,null);
				mHolder = new Holder();
				mHolder.label = (TextView)convertView.findViewById(R.id.tv_label);
				convertView.setTag(mHolder);
			}else
				mHolder = (Holder) convertView.getTag();
			
			//设置数据
			mHolder.label.setText((String)((Map<String,Object>)getItem(position)).get("title"));
			return convertView;
		}
		class Holder{
			TextView label;
		}
	}

}
